#ifndef CONNECTION_H
#define CONNECTION_H

#include <QObject>
#include <QSslSocket>
#include <QSslKey>
#include <QMutexLocker>
#include <QMutex>
#include "databaseinterface.h"

/**
  Represents a client connection at the Application layer.
  Reads client requests sent over SSL and responds.
  */
class Connection : public QObject
{
    Q_OBJECT
public:
    Connection(QObject *parent = 0, QSslSocket *socket = 0);

signals:
    void logInAttempt(const QString &, const QString &);
    void requestLogs(QStringList *);
    void newLog(const QString &userName, const QString &log);
    void accessListRequest(const QString & userName);
    void sslError(const QString &);
    void newUserRequest(const QString &, const QString &, int); // *
    void removeUserRequest(const QString &); // *
    void addAccessRequest(const QString &, const QString &);
    void removeAccessRequest(const QString &, const QString &);
    void disconnected();

public slots:
    void loginReply(bool, int privilege);
    void sslErrors(const QList<QSslError> &);
    void setupComms();
    void readFromSocket();
    void encrypted();
    void sendLog(const QString &userName, const QString &log, const QString &timeStamp);
    void queryDone();
    void newLogSuccess(const QString &);
    void newLogFailed();
    void accessListSuccess(QStringList *);
    void accessListFailure(const QString &reason);
    void addUserSuccess(const QString &name);
    void addUserFailure(const QString &reason);
    void removeUserSuccess();
    void removeUserFailed(const QString &reason);
    void addAccessSuccess();
    void addAccessFailed(const QString &reason);
    void removeAccessSuccess();
    void removeAccessFailed(const QString &reason);
    void socketStateChanged(QAbstractSocket::SocketState);

private:
    QSslSocket *socket;
    DatabaseInterface *db;

    bool clientConnected;
    bool adminConnected;

    QString certPath;
    QString keyPath;

    void checkLoginData(QStringList *);
};

#endif // CONNECTION_H
